<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<script src="/static/admin/js/math.js"></script>
<link rel="stylesheet" type="text/css" href="/static/admin/css/main.css">
<style>
    .layui-table td, .layui-table th{text-align: center!important;padding: 0!important;height: 40px}
    .bak{background: #fafafa!important;}
    #list_list input{text-align: center}
    .colorhong{cursor: pointer;}
    .layui-form-select dl dd.layui-select-tips{padding-left: 0!important;}
    .layui-form-select dl dd{padding: 0!important;}
    /*::-webkit-scrollbar{*/
        /*width:0;*/
    /*}*/
    #curthead tr th{font-weight: bold}
    #list_list tr td{font-size: 13px!important;}
</style>
<body class="laytp-container">
<div style="height: 45px"></div>
<div id="opt_nav">
</div>
<div id="dialog">
    <div class="page-title" id="page-title">
        <div class="page-title-con">
            <span class="title" id="titss"></span>
            <div class="move_title"></div>
            <div class="page-title-opt-list">
                <span id="page-close" class="page-close" onclick="layer_close()">×</span>
            </div>
        </div>
    </div>
</div>
<?php include "dao.php"?>

<div class="layui-card" id="table-card">
    <div class="layui-card-body">
        <div class="layui-row">
            <div class="layui-form layui-form-pane" id="commentForm">
                <div class="main-container" style="margin:0!important;padding: 0 10px!important;">
                    <div class="layui-form-item height_auto" style="padding: 0!important;">
                        <div class="short_table_list" key="1">
                            <div style="padding: 10px 0 5px 0;">
                                <button type="button" onclick="saveinfo()" class="layui-btn layui-btn-sm btn-dark marginr5">保存</button>
                                <div class="biantit" style="color: #248bfe;font-size: 13px;float: right;margin-right: 10px"></div>
                            </div>
                            <div>
                                <table class="layui-table" lay-size="sm">
                                    <thead id="curthead">
                                    <tr>
                                        <th style="width:45px">序号</th>
                                        <th>项目名称</th>
                                        <th>单位</th>
                                        <th>建筑面积</th>
                                        <th>模板展开<br/>总工程量</th>
                                        <th>单方含量<br/>（m2/m2）</th>
                                        <th>层数/根数</th>
                                        <th>配模套数</th>
                                        <th>残值</th>
                                        <th>配模的量</th>
                                        <th>不含税单价</th>
                                        <th>合价</th>
                                        <th>折到每平方接<br/>触面积的价格</th>
                                        <th>备注</th>
                                    </tr>
                                    </thead>
                                    <tbody class="short_table_tbody" id="list_list" style="max-height: 500px"></tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-col-md12" style="margin-top: 20px">
                <div class="layui-form-item" style="margin-bottom: 50px;">
                    <label for="phone" class="layui-form-label pl">方案说明：</label>
                    <div class="layui-input-inline opt_width" style="width: 100%;margin-right: 0;">
                        <textarea name="week_stuff" id="week_stuff" class="layui-textarea"></textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;

        facade.ajax({url: ask_main_week_stuff_list,data:{enginid:param_enginid}}).done(function(res){
            if (res.code === 2000) {
                $(".biantit").html(res.data.tit);
                $('#week_stuff').val(res.data.week_stuff);


                // 初始化数据
                var data = res.data.list;
                var str = '';
                var all_template = all_money = all_pin_money = 0;
                for(var j=0;j<data.length;j++){
                    str += '<tr>';
                    str += '<td style="width:45px"></td>';
                    str += '<td>'+data[j].qingname+'</td>';
                    str += '<td></td>';
                    str += '<td>'+data[j].area+'</td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '</tr>';

                    var child = data[j].children;
                    var key = 0;
                    for(var i=0;i<child.length;i++){
                        if(child[i].qingid){
                            if(key == 0){
                                all_template += child[i].template;
                                key += 1;
                            }
                            str += '<tr>';
                            str += '<td style="width:45px">'+(i + 1)+'</td>';
                            str += '<td class="qing" key="'+child[i].qingid+'">'+child[i].name+'</td>';
                            str += '<td class="unit">'+child[i].unit+'</td>';

                            if(child[i].name === '木模板材料费'){
                                str += '<td><input type="number" class="layui-input area" value="'+child[i].area+'"></td>';
                                str += '<td><input type="number" class="layui-input template" value="'+child[i].template+'"></td>';
                                str += '<td><input type="number" class="layui-input content bak" readonly value="'+child[i].content+'"></td>';
                                str += '<td><input type="number" class="layui-input ceng" value="'+child[i].ceng+'"></td>';
                                str += '<td><input type="number" class="layui-input num" value="'+child[i].num+'"></td>';
                                str += '<td><input type="number" class="layui-input chan" value="'+child[i].chan+'"></td>';
                                str += '<td><input type="number" class="layui-input mold bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input no_price" value="'+child[i].no_price+'"></td>';
                            }else if(child[i].name === '木方材料费'){
                                str += '<td><input type="number" class="layui-input area bak" readonly value="'+child[i].area+'"></td>';
                                str += '<td><input type="number" class="layui-input template bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input content bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input ceng" value="'+child[i].ceng+'"></td>';
                                str += '<td><input type="number" class="layui-input num" value="'+child[i].num+'"></td>';
                                str += '<td><input type="number" class="layui-input chan bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input mold bak" readonly value="'+child[i].mold+'"></td>';
                                str += '<td><input type="number" class="layui-input no_price" value="'+child[i].no_price+'"></td>';
                            }else if(child[i].name === '支撑体系'){
                                str += '<td><input type="number" class="layui-input area bak" readonly value="'+child[i].area+'"></td>';
                                str += '<td><input type="number" class="layui-input template bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input content bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input ceng bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input num bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input chan bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input mold bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input no_price bak" readonly value=""></td>';
                            }else if(child[i].name === '辅材'){
                                str += '<td><input type="number" class="layui-input area bak" readonly value="'+child[i].area+'"></td>';
                                str += '<td><input type="number" class="layui-input template bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input content bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input ceng bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input num bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input chan bak" readonly value=""></td>';
                                str += '<td><input type="number" class="layui-input mold bak" readonly value="'+child[i].mold+'"></td>';
                                str += '<td><input type="number" class="layui-input no_price" value="'+child[i].no_price+'"></td>';
                            }

                            str += '<td><input type="number" class="layui-input money bak" readonly value="'+child[i].money+'"></td>';
                            str += '<td><input type="number" class="layui-input pin_money bak" readonly value="'+child[i].pin_money+'"></td>';
                            str += '<td><input type="text" class="layui-input explain" value="'+child[i].explain+'"></td>';
                            str += '</tr>';
                        }else{
                            str += '<tr>';
                            str += '<td style="width: 45px"></td>';
                            str += '<td>'+child[i].name+'</td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td></td>';
                            str += '<td id="he'+child[i-1].qingid+'">'+child[i].money+'</td>';
                            str += '<td id="pin'+child[i-1].qingid+'">'+parseFloat(child[i].pin_money).toFixed(2)+'</td>';
                            str += '<td></td>';
                            str += '</tr>';
                            all_money += parseFloat(child[i].money);
                        }
                    }
                }

                if(all_money == 0){
                    all_pin_money = 0;
                }else{
                    if(all_template != 0){
                        all_pin_money = all_money / all_template;
                    }else{
                        all_pin_money = 0;
                    }
                }

                str += '<tr>';
                str += '<td style="width:45px"></td>';
                str += '<td>总计</td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td id="all_template">'+all_template.toFixed(2)+'</td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td></td>';
                str += '<td id="all_money">'+all_money.toFixed(2)+'</td>';
                str += '<td id="all_pin_money">'+all_pin_money.toFixed(2)+'</td>';
                str += '<td></td>';
                str += '</tr>';

                $('#list_list').html(str);
                render_from();
                jisuan();
            }
        });

    });

    function saveinfo(){
        var list = [''];
        $('#list_list tr').each(function (i) {
            var qingid = $(this).find('.qing').attr('key');
            if(qingid){
                var name = $(this).find('.qing').text();
                var unit = $(this).find('.unit').text();
                var area = $(this).find('.area').val();
                var template = $(this).find('.template').val();
                var content = $(this).find('.content').val();
                var mold = $(this).find('.mold').val();
                var ceng = $(this).find('.ceng').val();
                var num = $(this).find('.num').val();
                var chan = $(this).find('.chan').val();
                var no_price = $(this).find('.no_price').val();
                var money = $(this).find('.money').val();
                var pin_money = $(this).find('.pin_money').val();
                var explain = $(this).find('.explain').val();

                list.push({
                    enginid:param_enginid,
                    qingid:qingid,
                    name:name,
                    unit:unit,
                    area:area,
                    template:template,
                    content:content,
                    mold:mold,
                    ceng:ceng,
                    num:num,
                    chan:chan,
                    no_price:no_price,
                    money:money,
                    pin_money:pin_money,
                    explain:explain
                });
            }
        });
        if(list.length == 1){facade.error('请填写明细'); return false;}


        var week_stuff = $('#week_stuff').val();
        facade.ajax({url: ask_main_week_stuff_edit, data:{enginid:param_enginid, list:list,week_stuff:week_stuff}}).done(function(res){
            if (res.code === 2000) {
                facade.success("提交成功!");
                setTimeout(() => {
                    window.location.reload();
                }, 1500);
            }
        });
    }




    $('#list_list').on('input propertychange','.area,.template,.mold,.no_price,.num,.ceng,.chan',function () {
        if($(this).parent().parent().find('.qing').text() == '木模板材料费'){
            var area = $(this).parent().parent().find('.area').val() || 0;
            var template = $(this).parent().parent().find('.template').val() || 0;
            var no_price = $(this).parent().parent().find('.no_price').val() || 0;
            var ceng = $(this).parent().parent().find('.ceng').val() || 0;
            var num = $(this).parent().parent().find('.num').val() || 0;
            var chan = $(this).parent().parent().find('.chan').val() || 0;

            if(parseFloat(area) > 0){
                var content = (parseFloat(template) / parseFloat(area)).toFixed(2);
                $(this).parent().parent().find('.content').val(content);
            }

            var mold = 0;
            if(ceng != 0){
                mold = (parseFloat(template) / parseFloat(ceng) * parseFloat(num) * (1-parseFloat(chan))).toFixed(2);
            }
            $(this).parent().parent().find('.mold').val(mold);

            var money = (parseFloat(mold) * parseFloat(no_price)).toFixed(2);
            $(this).parent().parent().find('.money').val(money);

            if(parseFloat(template) > 0){
                var pin_money = (parseFloat(money) / parseFloat(template)).toFixed(2);
                $(this).parent().parent().find('.pin_money').val(pin_money);
            }


            // 处理 辅材 模板的量
            var key = $(this).parent().parent().find('.qing').attr('key');
            $('#list_list tr').each(function () {
                if($(this).find('.qing').text() == '木方材料费' && $(this).find('.qing').attr('key') == key){
                    $(this).find('.area').val(area);
                    var num = $(this).find('.num').val() || 0;
                    var no_price = $(this).find('.no_price').val() || 0;
                    var ceng = $(this).find('.ceng').val() || 0;


                    var mold = 0;
                    if(num != 0){
                        mold = (parseFloat(area) * parseFloat(ceng) / parseFloat(num)).toFixed(2);
                    }
                    $(this).find('.mold').val(mold);

                    var money = parseFloat(mold) * parseFloat(no_price);
                    $(this).find('.money').val(money.toFixed(2));

                    var pin_money = 0;
                    if(template != 0 && money != 0){
                        pin_money = parseFloat(money) / parseFloat(template);
                    }
                    $(this).find('.pin_money').val(pin_money.toFixed(2));
                }

                if($(this).find('.qing').text() == '辅材' && $(this).find('.qing').attr('key') == key){
                    $(this).find('.mold').val(template);
                    $(this).find('.area').val(area);
                }

                if($(this).find('.qing').text() == '支撑体系' && $(this).find('.qing').attr('key') == key){
                    $(this).find('.area').val(area);
                    var money = $(this).find('.money').val() || 0;
                    if(parseFloat(template) > 0){
                        var pin_money = (parseFloat(money) / parseFloat(template)).toFixed(2);
                    }else{
                        var pin_money = 0;
                    }
                    $(this).find('.pin_money').val(pin_money);
                }
            })
        }

        if($(this).parent().parent().find('.qing').text() == '木方材料费'){
            var area = $(this).parent().parent().find('.area').val() || 0;
            var num = $(this).parent().parent().find('.num').val() || 0;
            var no_price = $(this).parent().parent().find('.no_price').val() || 0;
            var ceng = $(this).parent().parent().find('.ceng').val() || 0;
            var key = $(this).parent().parent().find('.qing').attr('key');
            // 木模板材料费的值
            var template = 0;
            $('#list_list tr').each(function () {
                if($(this).find('.qing').text() == '木模板材料费' && $(this).find('.qing').attr('key') == key){
                    template = $(this).find('.template').val();
                }
            });

            var mold = 0;
            if(num != 0){
                mold = (parseFloat(area) * parseFloat(ceng) / parseFloat(num)).toFixed(2);
            }
            $(this).parent().parent().find('.mold').val(mold);

            var money = parseFloat(mold) * parseFloat(no_price);
            $(this).parent().parent().find('.money').val(money.toFixed(2));

            var pin_money = 0;
            if(template != 0 && money != 0){
                pin_money = parseFloat(money) / parseFloat(template);
            }
            $(this).parent().parent().find('.pin_money').val(pin_money.toFixed(2));
        }

        if($(this).parent().parent().find('.qing').text() == '辅材'){
            var mold = $(this).parent().parent().find('.mold').val() || 0;
            var no_price = $(this).parent().parent().find('.no_price').val() || 0;
            var key = $(this).parent().parent().find('.qing').attr('key');
            // 木模板材料费的值
            var template = 0;
            $('#list_list tr').each(function () {
                if($(this).find('.qing').text() == '木模板材料费' && $(this).find('.qing').attr('key') == key){
                    template = $(this).find('.template').val();
                }
            });

            var money = parseFloat(mold) * parseFloat(no_price);
            $(this).parent().parent().find('.money').val(money.toFixed(2));

            var pin_money = 0;
            if(template != 0 && money != 0){
                pin_money = parseFloat(money) / parseFloat(template);
            }
            $(this).parent().parent().find('.pin_money').val(pin_money.toFixed(2));
        }

        // 处理合计
        var key = $(this).parent().parent().find('.qing').attr('key');
        jisuan(key);
    });


    function jisuan(key) {
        var money_all = 0; var pin_money_all = 0;
        $('#list_list tr').each(function () {
            if(key == $(this).find('.qing').attr('key')){
                var money = $(this).find('.money').val();
                if(money != ''){
                    money_all += parseFloat(money);
                }
                var pin_money = $(this).find('.pin_money').val();
                if(pin_money != ''){
                    pin_money_all += parseFloat(pin_money);
                }
            }
        });
        $('#he'+key).text(money_all.toFixed(2));
        $('#pin'+key).text(pin_money_all.toFixed(2));

        // 计算总计
        var all_template = 0; var all_money = 0;
        $('#list_list tr').each(function () {
            let key = $(this).find('.qing').attr('key');
            if($(this).find('td:first-child').text() == '1'){
                let template = $(this).find('.template').val() || 0;
                all_template += parseFloat(template);
                let key = $(this).find('.qing').attr('key');
                let money = $('#he'+key).text() || 0;
                all_money += parseFloat(money);
            }
        });
        var all_pin_money = 0;
        if(all_money != 0){
            all_pin_money = all_money / all_template;
        }
        $('#all_template').text(all_template.toFixed(2));
        $('#all_money').text(all_money.toFixed(2));
        if(all_pin_money == 'Infinity'){
            all_pin_money = 0;
        }
        $('#all_pin_money').text(all_pin_money.toFixed(2));
    }
</script>
</body>
</html>
